<!-- 小程序编辑弹窗 -->
<template>
  <el-dialog width="750px" :visible="visible" :lock-scroll="false" :destroy-on-close="true" custom-class="ele-dialog-form"
    :title="isUpdate ? '修改小程序' : '添加小程序'" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <!-- <el-form-item label="身份证正面：">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item> -->
      <el-form-item label="授权链接:" prop="auth_mode">
        <el-link type="primary" :href="this.form.auth_url">跳转授权链接</el-link>
        <div>{{ this.form.auth_url }}</div>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消
      </el-button>
      <el-button type="primary" :loading="loading" @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'appletEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  
  data() {
    return {
      // 表单数据
      form: Object.assign({  }, this.data),
      // 表单验证规则
      rules: {
        merchant_name: [
          { required: true, message: '请输入营业执照名称', trigger: 'blur' }
        ],
        license_number: [
          { required: true, message: '请输入统一社会信用代码', trigger: 'blur' }
        ],
        legal_persona_name: [
          { required: true, message: '请输入所有者姓名', trigger: 'blur' }
        ],
        legal_persona_wechat: [
          { required: true, message: '请输入所有者微信', trigger: 'blur' }
        ],
        component_phone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        // password: [
        //   {required: true, pattern: /^[\S]{5,18}$/, message: '密码必须为5-18位非空白字符', trigger: 'blur'}
        // ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      console.log(this.data)
      if (this.data) {
        this.form = Object.assign({}, this.data, {
        });
        this.isUpdate = true;
      } else {
        this.form = {
        };
        this.isUpdate = false;
      }
    }
  },
  mounted() {
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 城市数据处理
          this.$http.post('/applet/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({ type: 'success', message: res.data.msg });
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
  }
}
</script>
<style scoped></style>
